/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.method;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 *
 * @param <T> < p/> uthor EnzoZhong
 * @date 2011-11-20
 */
public class Insieme<T> {

      /**
       *
       * @param <T>
       * @param col1
       * @param col2
       *
       * @return
       */
      public static <T> List<T> union ( List<T> col1 , List<T> col2 ) {
            return To.set2List ( privateUnion ( col1 , col2 ) );
      }

      private static <T> Set<T> privateUnion ( Collection<T> col1 , Collection<T> col2 ) {
            HashSet<T> hashSet = new HashSet<> ( 20 );
            col1.addAll ( col2 );
            hashSet.addAll ( col1 );
            return hashSet;
      }

      /**
       *
       * @param <T>
       * @param list1
       * @param list2
       *
       * @return
       */
      public synchronized static <T> List<T> inter ( List<T> list1 , List<T> list2 ) {
            T[] array1 = To.list2Array ( list1 );
            T[] array2 = To.list2Array ( list2 );
            T[] result = inter ( array1 , array2 );
            return To.array2List ( result );
      }

      /**
       * 如果2者都为空，那么交集为空 如果2者有一为空，那么交集为空 如果2者都不为空，那么就要求交集
       *
       * @param <T>
       * @param a1
       * @param a2
       *
       * @return
       */
      @SuppressWarnings ( "unchecked" )
      public synchronized static <T> T[] inter ( T[] a1 , T[] a2 ) {
            Boolean booleanA1 = Self.isNull ( a1 );
            Boolean booleanA2 = Self.isNull ( a2 );
            //2者都为空
            if ( booleanA1 && booleanA2 ) {
                  return ( T[] ) new Object[ 0 ];
            }
            //2者之间有1为空
            if ( booleanA1 || booleanA2 ) {
                  return ( T[] ) new Object[ 0 ];
            }
            //2者都不为空
            if (  ! booleanA1 &&  ! booleanA2 ) {
                  Integer len1 = a1 == null ? 0 : a1.length;
                  Integer len2 = a2 == null ? 0 : a2.length;
                  int len = Math.max ( len1 , len2 );
                  Class<?> clazz = a1 == null ? a2[0].getClass () : a1[0].getClass ();
                  T[] array = Maker.makeArray ( clazz , len );
                  int index = 0;
                  for ( T t : a1 ) {
                        if ( has ( t , a2 ) ) {
                              array[index] = t;
                              index ++;
                        }
                  }
                  return Self.subArray ( array , 0 , index );
            }
            return null;
      }

      /**
       *
       * @param array1
       * @param array2
       *
       * @return
       */
      public T[] comple ( T[] array1 , T[] array2 ) {
            throw new UnsupportedOperationException ( "Not supported yet." );
      }

      /**
       *
       * @param array1
       * @param array2
       *
       * @return
       */
      public Boolean contain ( T[] array1 , T[] array2 ) {
            throw new UnsupportedOperationException ( "Not supported yet." );
      }

      /**
       *
       * @param <T>
       * @param t
       * @param array
       *
       * @return
       */
      public synchronized static <T> Boolean has ( T t , T[] array ) {
            for ( T tmp : array ) {
                  if ( t.equals ( tmp ) ) {
                        return true;
                  }
            }
            return false;
      }

      public static <V , K> Map<V , K> swop ( Map<K , V> map ) {
            HashMap<V , K> reMap = new HashMap<> ( 20 );
            Set<Entry<K , V>> set = map.entrySet ();
            for ( Entry<K , V> entry : set ) {
                  K k = entry.getKey ();
                  V v = entry.getValue ();
                  reMap.put ( v , k );
            }
            return reMap;
      }

}
